﻿using System;
using System.Data;
using System.Data.Objects;
using System.Web.Security;
using SBACN.WebSite.Models;
using SBACN.WebSite.Code;
using SBACN.WebSite.Cache;

namespace SBACN.WebSite.Admin
{
	public partial class Login : BasePage
	{
		protected void Page_Load(object sender, EventArgs e)
		{

		}

		protected void btn_Login_Click(object sender, EventArgs e)
		{
			if(string.IsNullOrWhiteSpace(txt_UserName.Text))
			{
				this.lbl_Msg.Text = "用户名不能为空!";
				return;
			}
			string UserName = txt_UserName.Text.Trim();

			string emptyMD5 = FormsAuthentication.HashPasswordForStoringInConfigFile(string.Empty, "MD5").ToLower();
			if (string.IsNullOrWhiteSpace(hid_Password.Value) || hid_Password.Value.Trim().ToLower() == emptyMD5)
			{
				this.lbl_Msg.Text = "密码不能为空!";
				return;
			}
			string password = FormsAuthentication.HashPasswordForStoringInConfigFile(hid_Password.Value.Trim().ToLower(), "MD5").ToLower();
			TB_User user = DAL.DALHelper.Instance.GetObject<TB_User>("UserName", UserName);
			if (user != null && user.Password == password && user.MemberID == -1)
			{
				if (user.Status && UserRightCache.Instance[user.UserName].Count > 0)
				{
					user.LastLoginIP = user.LoginIP;
					user.LastLoginTime = user.LoginTime;
					user.LoginIP = BasePage.GetRequestIP(Request);
					user.LoginTime = DateTime.Now;
					user.LoginCount++;
					DAL.DALHelper.Instance.Update(user);
					DAL.DALHelper.Instance.SaveChanges();
					Session["AdminInfo"] = user;
					this.lbl_Msg.Text = string.Empty;

					Response.Redirect(AdminDefaultPage, true);
				}
				else
				{
					this.lbl_Msg.Text = "禁止登录!";
				}
			}
			else
			{
				this.lbl_Msg.Text = "用户名、密码不匹配!";
			}
		}
	}
}